package com.gombe.credit.app.ui.adapter

import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.gombe.credit.app.R
import com.gombe.credit.lib.adapter.GombeCreditOnSingleClickListener
import com.gombe.credit.lib.adapter.GombeCreditRecyclerViewAdapter
import com.gombe.credit.lib.adapter.GombeCreditRecyclerViewHolder
import com.gombe.credit.lib.adapter.GombeCreditViewHolderHelper
import com.gombe.credit.lib.shapeview.view.GombeCreditView
import org.json.JSONObject

/**
 * GombeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/27 13:45
 * @Annotation：在线客服会话
 */
class AdapterGombeCreditUserChat(recyclerView: RecyclerView?): GombeCreditRecyclerViewAdapter<JSONObject>(recyclerView) {

    //快捷发送问题点击回调
    private var guessItemListener: IChatGuessClickListener? = null
    fun setChatGuessItemClickListener(listener: IChatGuessClickListener){
        guessItemListener = listener
    }

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        val itemType = model.optString("itemType")
        return when(itemType){
            "itemSend" -> {
                R.layout.act_gombe_credit_user_chat_send_item
            }

            "itemReceived" -> {
                R.layout.act_gombe_credit_user_chat_received_item
            }

            "itemGuess" -> {
                R.layout.act_gombe_credit_user_chat_guess_item
            }
            //"itemWait"
            else -> {
                R.layout.act_gombe_credit_user_chat_send_item
//                R.layout.act_gombe_credit_user_chat_wait_item
            }
        }
    }
    override fun setItemChildListener(helper: GombeCreditViewHolderHelper, viewType: Int) {
    }

    override fun onBindViewHolder(holder: GombeCreditRecyclerViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.isEmpty()){
            onBindViewHolder(holder,position)
            return
        }
        val helper = holder.viewHolderHelper
        val model = getItem(position) as JSONObject
        val itemType = model.optString("itemType")
        if (itemType == "itemSend"){
            showChatSendView(helper,position,model)
        }
    }

    override fun onBindViewHolder(viewHolder: GombeCreditRecyclerViewHolder, position: Int) {
        super.onBindViewHolder(viewHolder, position)
        val helper = viewHolder.viewHolderHelper
        val model = getItem(position) as JSONObject
        when(val itemType = model.optString("itemType")){
            "itemSend",
            "itemReceived" -> {
                helper.setText(R.id.textChatMsg,model.optString("talkInfo"))

                if (itemType == "itemSend"){
                    showChatSendView(helper,position,model)
                }
            }
            "itemGuess" -> {
                showChatGuessView(helper,position,model)
            }
        }
    }

    override fun fillData(helper: GombeCreditViewHolderHelper, position: Int, model: JSONObject) {
//        when(val itemType = model.optString("itemType")){
//            "itemSend",
//            "itemReceived" -> {
//                helper.setText(R.id.textChatMsg,model.optString("talkInfo"))
//
//                if (itemType == "itemSend"){
//                    showChatSendView(helper,position,model)
//                }
//            }
//        }
    }

    //发送
    private fun showChatSendView(helper: GombeCreditViewHolderHelper, position: Int, model: JSONObject){
        helper.setVisibility(R.id.readPointLayout, View.GONE)
        helper.setVisibility(R.id.textChatWait, View.GONE)
        //最后一条消息 && 发送
        if (position == itemCount - 1){
            helper.setVisibility(R.id.readPointLayout, View.VISIBLE)
            val readPoint1 = helper.getView<GombeCreditView>(R.id.readPoint1)
            val readPoint2 = helper.getView<GombeCreditView>(R.id.readPoint2)

            val createTimeStamp = model.optString("createTimeStamp","0")
            val intervalTime = (System.currentTimeMillis() - createTimeStamp.toLong()) / 1000
            if (intervalTime >= 10){
                readPoint1.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_FF00C471))
                    .intoBackground()
                readPoint2.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_FF00C471))
                    .intoBackground()
                helper.setVisibility(R.id.textChatWait, View.VISIBLE)
            } else if (intervalTime in 6..10){
                readPoint1.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_FF00C471))
                    .intoBackground()
                readPoint2.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_ffc8c8c8))
                    .intoBackground()
            } else {
                readPoint1.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_ffc8c8c8))
                    .intoBackground()
                readPoint2.shapeDrawableBuilder
                    .setSolidColor(getColor(R.color.gombe_credit_ffc8c8c8))
                    .intoBackground()
            }
        }
    }

    /**
     * Guess
     * 快捷发送
     */
    private fun showChatGuessView(helper: GombeCreditViewHolderHelper, position: Int, model: JSONObject){
        val question1Layout = helper.getView<LinearLayout>(R.id.question1Layout)
        question1Layout.setOnClickListener(IQuestionGuessClickListener)

        val question2Layout = helper.getView<LinearLayout>(R.id.question2Layout)
        question2Layout.setOnClickListener(IQuestionGuessClickListener)

        val question3Layout = helper.getView<LinearLayout>(R.id.question3Layout)
        question3Layout.setOnClickListener(IQuestionGuessClickListener)

        val question4Layout = helper.getView<LinearLayout>(R.id.question4Layout)
        question4Layout.setOnClickListener(IQuestionGuessClickListener)

        val question5Layout = helper.getView<LinearLayout>(R.id.question5Layout)
        question5Layout.setOnClickListener(IQuestionGuessClickListener)

        val question6Layout = helper.getView<LinearLayout>(R.id.question6Layout)
        question6Layout.setOnClickListener(IQuestionGuessClickListener)
    }

    private val IQuestionGuessClickListener = object : GombeCreditOnSingleClickListener(){
        override fun onSingleClick(v: View?) {
            when(v?.id){
                R.id.question1Layout -> {
                    guessItemListener?.onGuessItemClick(getString(R.string.gombe_credit_user_chat_question1))
                }
                R.id.question2Layout -> {
                    guessItemListener?.onGuessItemClick(getString(R.string.gombe_credit_user_chat_question2))
                }
                R.id.question3Layout -> {
                    guessItemListener?.onGuessItemClick(getString(R.string.gombe_credit_user_chat_question3))
                }
                R.id.question4Layout -> {
                    guessItemListener?.onGuessItemClick(getString(R.string.gombe_credit_user_chat_question4))
                }
                R.id.question5Layout -> {
                    guessItemListener?.onGuessItemClick(getString(R.string.gombe_credit_user_chat_question5))
                }
                R.id.question6Layout -> {
                    guessItemListener?.onGuessItemClick(getString(R.string.gombe_credit_user_chat_question6))
                }
            }
        }
    }

    interface IChatGuessClickListener{
        fun onGuessItemClick(text: String)
    }
}